package com.zhipu.balance.ui.vm.home.record

import android.text.TextUtils
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.SPUtils
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.home.record.RecordActivity
import com.zhipu.balance.ui.home.record.RecordRiceFragment
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.InitializeUtils
import com.zhipu.balance.utils.data.AverageValueUtils
import com.zhipu.balance.utils.dialog.CommonDialog
import com.zhipu.balance.utils.print.corn.CornPrintUtils
import com.zhipu.balance.utils.print.dami.DaMiPrintUtils
import com.zhipu.balance.utils.print.rapeseed.RapeseedPrintUtils
import com.zhipu.balance.utils.print.rice.RicePrintUtils
import com.zhipu.balance.utils.print.soybean.SoybeanPrintUtils
import com.zhipu.balance.utils.print.wheat.WheatPrintUtils
import com.zhipu.balance.utils.upload.corn.CornUploadUtils
import com.zhipu.balance.utils.upload.dami.DaMiUploadUtils
import com.zhipu.balance.utils.upload.rapeseed.RapeseedUploadUtils
import com.zhipu.balance.utils.upload.rice.RiceUploadUtils
import com.zhipu.balance.utils.upload.soybean.SoybeanUploadUtils
import com.zhipu.balance.utils.upload.wheat.WheatUploadUtils

/**
 * 使用记录VM
 */
class RecordViewModel(mSystemApiService: SystemApiService) : BaseViewModel(mSystemApiService) {

    /**
     * 主类
     */
    var mMainClass = MutableLiveData("稻谷")

    /**
     * 子类集合
     */
    var mSubclassList = MutableLiveData<List<String>>()

    /**
     * 子类
     */
    var mSubclass = MutableLiveData("杂质")

    /**
     *  当前页数
     */
    var atPresentPage = MutableLiveData(1)
    var atPresentPage1 : Int = 1



    /**
     *  不完善细分
     */
    var mImperfectSubdivision : MutableLiveData<Boolean> = MutableLiveData(SPUtils.getInstance().getBoolean("ImperfectSubdivision",false))


    /**
     * 查询
     */
    var uniqueId: String = ""


    /**
     * 位置
     */
    var mPosition = MutableLiveData(0)

    /**
     *  Fragment集合
     */
    var mFragments = MutableLiveData<List<Fragment>>()

    /**
     * 使用稻谷记录
     */
    var mRecordRiceFragment = RecordRiceFragment()

    /**
     * 总页数
     */
    var alwaysPage = MutableLiveData(1)


    init {
        /**
         * Fragment适配器
         */
        var mFragmentList = mutableListOf<Fragment>()
        //使用稻谷记录
        mFragmentList.add(mRecordRiceFragment)


        /**
         * Fragment集合
         */
        mFragments.postValue(mFragmentList)
        /**
         * 子类
         */
        mSubclass.postValue("杂质")
    }


    /**
     * 更新数据
     */
    fun UpdateDataMainClass() {
        when (mMainClass.value) {
            "稻谷" -> {
                val mStringList: MutableList<String> = ArrayList()
                mStringList.add("杂质")
                mStringList.add("出糙")
                mStringList.add("整精米")
                mStringList.add("谷外糙米")
                mStringList.add("黄粒米")
                mStringList.add("互混")
                mStringList.add("出米率")
                //子类
                mSubclassList.postValue(mStringList)
            }
            "小麦" -> {
                val mStringList: MutableList<String> = ArrayList()
                mStringList.add("杂质")
                mStringList.add("不完善")
                mStringList.add("面筋")
                mStringList.add("容重")
                //子类
                mSubclassList.postValue(mStringList)
            }
            "玉米" -> {
                val mStringList: MutableList<String> = ArrayList()
                mStringList.add("杂质")
                mStringList.add("不完善")
                mStringList.add("容重")
                //子类
                mSubclassList.postValue(mStringList)
            }
            "大米" -> {
                val mStringList: MutableList<String> = ArrayList()
                mStringList.add("杂质")
                mStringList.add("不完善")
                mStringList.add("碎米率")
                mStringList.add("黄粒米")
                mStringList.add("互混")
                //子类
                mSubclassList.postValue(mStringList)
            }
            "大豆" -> {
                val mStringList: MutableList<String> = ArrayList()
                mStringList.add("杂质")
                mStringList.add("损伤粒率")
                mStringList.add("热损伤粒率")
                mStringList.add("完整粒率")
                //子类
                mSubclassList.postValue(mStringList)
            }
            "油菜籽" -> {
                val mStringList: MutableList<String> = ArrayList()
                mStringList.add("杂质")
                mStringList.add("生芽粒")
                mStringList.add("生霉粒")
                mStringList.add("热损伤粒")
                mStringList.add("未熟粒")
                //子类
                mSubclassList.postValue(mStringList)
            }
        }
        /**
         * 子类
         */
        mSubclass.postValue("杂质")

        mRecordRiceFragment.UpdateDataMainClass(mMainClass.value.toString())
    }


    /**
     * 更新子类数据
     */
    fun UpdateDataSubclass() {
        mRecordRiceFragment.UpdateDataSubclass(mSubclass.value.toString())
        //总页数
        alwaysPage.postValue(mRecordRiceFragment.alwaysPage)
    }

    /**
     * 查询
     */
    fun query() {
        mRecordRiceFragment.setUniqueId(uniqueId)
    }

    /**
     * 上一页
     */
    fun PreviousPage(){
        //总页数
        alwaysPage.postValue(mRecordRiceFragment.alwaysPage)
        when(atPresentPage1){
            1 ->{
                InitializeUtils.getDefaultMaker()?.show("最上页了")
            }
            else ->{
                atPresentPage1 --
                atPresentPage.postValue(atPresentPage1)
                mRecordRiceFragment.page -= 9
                /**
                 * 分页
                 */
                mRecordRiceFragment.paging()
            }
        }
    }


    /**
     * 下一页
     */
    fun NextPage(){
        //总页数
        alwaysPage.postValue(mRecordRiceFragment.alwaysPage)
        //
        when(atPresentPage1 >= mRecordRiceFragment.alwaysPage){
            true ->{
                InitializeUtils.getDefaultMaker()?.show("最下页了")
            }
            false ->{
                atPresentPage1 ++
                atPresentPage.postValue(atPresentPage1)
                mRecordRiceFragment.page += 9
                /**
                 * 分页
                 */
                mRecordRiceFragment.paging()
            }
        }
    }

    /**
     * 选中
     */
    fun CheckBoxSelect(Select : Boolean){
        mRecordRiceFragment.CheckBoxSelect(Select)
    }



    /**
     * 计算平均值
     */
    fun CalculateAverage() {
        when (mMainClass.value) {
            "稻谷" -> {
                var uniqueId: String? = null
                var mDifferentChoices = false
                var length = 0
                for (mRecordRice in mRecordRiceFragment.mRecordRiceList){
                    if(mRecordRice.select){
                        length ++
                        //是否平均值
                        if(mRecordRice.impurityAverageValue
                            || mRecordRice.roughnessRateAverageValue
                            || mRecordRice.milledRiceAverageValue
                            || mRecordRice.ExternalValleyRoughnessAverageValue
                            || mRecordRice.YellowGrainRiceAverageValue
                            || mRecordRice.intermixingAverageValue
                            || mRecordRice.RiceOutputAverageValue){
                            InitializeUtils.getDefaultMaker()?.show("请不要选择已经计算过平均值数据")
                            return
                        }
                        if(TextUtils.isEmpty(uniqueId)){
                            uniqueId = mRecordRice.uniqueId
                        }else{
                            if(!mRecordRice.uniqueId.equals(uniqueId)){
                                mDifferentChoices = true;
                            }
                        }
                    }
                }
                if(length <= 1){
                    InitializeUtils.getDefaultMaker()?.show("请多选几条数据才能进行平均值计算")
                    return
                }
                if(mDifferentChoices){
                    InitializeUtils.getDefaultMaker()?.show("请不要选择不同的编号")
                }else{
                    var mRecordRice = AverageValueUtils.AverageValueRice(mRecordRiceFragment.mRecordRiceList)
                    if(null != mRecordRice){
                        mRecordRiceFragment.UpdateDataMainClass(mMainClass.value.toString())
                    }
                }
            }
            "小麦" -> {
                var uniqueId: String? = null
                var mDifferentChoices = false
                var length = 0
                for (mRecordWheat in mRecordRiceFragment.mRecordWheatList){
                    if(mRecordWheat.select){
                        length ++
                        //是否平均值
                        if(mRecordWheat.impurityAverageValue
                            || mRecordWheat.imperfectGrainValue
                            || mRecordWheat.glutenSampleValue
                            || mRecordWheat.densityAverageValue){
                            InitializeUtils.getDefaultMaker()?.show("请不要选择已经计算过平均值数据")
                            return
                        }
                        if(TextUtils.isEmpty(uniqueId)){
                            uniqueId = mRecordWheat.uniqueId
                        }else{
                            if(!mRecordWheat.uniqueId.equals(uniqueId)){
                                mDifferentChoices = true;
                            }
                        }
                    }
                }
                if(length <= 1){
                    InitializeUtils.getDefaultMaker()?.show("请多选几条数据才能进行平均值计算")
                    return
                }
                if(mDifferentChoices){
                    InitializeUtils.getDefaultMaker()?.show("请不要选择不同的编号")
                }else{
                    var mRecordWheat = AverageValueUtils.AverageValueRiceWheat(mRecordRiceFragment.mRecordWheatList)
                    if(null != mRecordWheat){
                        mRecordRiceFragment.UpdateDataMainClass(mMainClass.value.toString())
                    }
                }
            }
            "玉米" -> {
                var uniqueId: String? = null
                var mDifferentChoices = false
                var length = 0
                for (mRecordCorn in mRecordRiceFragment.mRecordCornList){
                    if(mRecordCorn.select){
                        length ++
                        //是否平均值
                        if(mRecordCorn.impurityAverageValue
                            || mRecordCorn.imperfectGrainValue
                            || mRecordCorn.densityAverageValue){
                            InitializeUtils.getDefaultMaker()?.show("请不要选择已经计算过平均值数据")
                            return
                        }
                        if(TextUtils.isEmpty(uniqueId)){
                            uniqueId = mRecordCorn.uniqueId
                        }else{
                            if(!mRecordCorn.uniqueId.equals(uniqueId)){
                                mDifferentChoices = true;
                            }
                        }
                    }
                }
                if(length <= 1){
                    InitializeUtils.getDefaultMaker()?.show("请多选几条数据才能进行平均值计算")
                    return
                }
                if(mDifferentChoices){
                    InitializeUtils.getDefaultMaker()?.show("请不要选择不同的编号")
                }else{
                    mSubclass.value?.let {
                        var mRecordCorn = AverageValueUtils.AverageValueRecordCorn(mRecordRiceFragment.mRecordCornList, it)
                        if(null != mRecordCorn){
                            mRecordRiceFragment.UpdateDataMainClass(mMainClass.value.toString())
                        }
                    }
                }
            }
            "大米" -> {
                var uniqueId: String? = null
                var mDifferentChoices = false
                var length = 0
                for (mRecordDaMi in mRecordRiceFragment.mRecordDaMiList){
                    if(mRecordDaMi.select){
                        length ++
                        //是否平均值
                        if(mRecordDaMi.impurityAverageValue
                            || mRecordDaMi.imperfectGrainValue
                            || mRecordDaMi.qualityOfBrokenValue
                            || mRecordDaMi.yellowGrainRiceSampleValue
                            || mRecordDaMi.massOfMixedSampleValue){
                            InitializeUtils.getDefaultMaker()?.show("请不要选择已经计算过平均值数据")
                            return
                        }
                        if(TextUtils.isEmpty(uniqueId)){
                            uniqueId = mRecordDaMi.uniqueId
                        }else{
                            if(!mRecordDaMi.uniqueId.equals(uniqueId)){
                                mDifferentChoices = true;
                            }
                        }
                    }
                }
                if(length <= 1){
                    InitializeUtils.getDefaultMaker()?.show("请多选几条数据才能进行平均值计算")
                    return
                }
                if(mDifferentChoices){
                    InitializeUtils.getDefaultMaker()?.show("请不要选择不同的编号")
                }else{
                    mSubclass.value?.let {
                        var mRecordDaMi = AverageValueUtils.AverageValueRecordDaMi(mRecordRiceFragment.mRecordDaMiList, it)
                        if(null != mRecordDaMi){
                            mRecordRiceFragment.UpdateDataMainClass(mMainClass.value.toString())
                        }
                    }
                }
            }
            "大豆" -> {
                var uniqueId: String? = null
                var mDifferentChoices = false
                var length = 0
                for (mRecordSoybean in mRecordRiceFragment.mRecordSoybeanList){
                    if(mRecordSoybean.select){
                        length ++
                        //是否平均值
                        if(mRecordSoybean.impurityAverageValue
                            || mRecordSoybean.thermalDamageValue
                            || mRecordSoybean.damagedGrainValue
                            || mRecordSoybean.immatureBrokenGrainValue){
                            InitializeUtils.getDefaultMaker()?.show("请不要选择已经计算过平均值数据")
                            return
                        }
                        if(TextUtils.isEmpty(uniqueId)){
                            uniqueId = mRecordSoybean.uniqueId
                        }else{
                            if(!mRecordSoybean.uniqueId.equals(uniqueId)){
                                mDifferentChoices = true;
                            }
                        }
                    }
                }
                if(length <= 1){
                    InitializeUtils.getDefaultMaker()?.show("请多选几条数据才能进行平均值计算")
                    return
                }
                if(mDifferentChoices){
                    InitializeUtils.getDefaultMaker()?.show("请不要选择不同的编号")
                }else{
                    mSubclass.value?.let {
                        var mRecordSoybean = AverageValueUtils.AverageValueRecordSoybean(mRecordRiceFragment.mRecordSoybeanList, it)
                        if(null != mRecordSoybean){
                            mRecordRiceFragment.UpdateDataMainClass(mMainClass.value.toString())
                        }
                    }
                }
            }
            "油菜籽" -> {
                var uniqueId: String? = null
                var mDifferentChoices = false
                var length = 0
                for (mRecordRapeseed in mRecordRiceFragment.mRecordRapeseedList){
                    if(mRecordRapeseed.select){
                        length ++
                        //是否平均值
                        if(mRecordRapeseed.impurityAverageValue
                            || mRecordRapeseed.sproutWeightValue
                            || mRecordRapeseed.moldWeightValue
                            || mRecordRapeseed.thermalDamageValue
                            || mRecordRapeseed.immatureGrainValue){
                            InitializeUtils.getDefaultMaker()?.show("请不要选择已经计算过平均值数据")
                            return
                        }
                        if(TextUtils.isEmpty(uniqueId)){
                            uniqueId = mRecordRapeseed.uniqueId
                        }else{
                            if(!mRecordRapeseed.uniqueId.equals(uniqueId)){
                                mDifferentChoices = true;
                            }
                        }
                    }
                }
                if(length <= 1){
                    InitializeUtils.getDefaultMaker()?.show("请多选几条数据才能进行平均值计算")
                    return
                }
                if(mDifferentChoices){
                    InitializeUtils.getDefaultMaker()?.show("请不要选择不同的编号")
                }else{
                    mSubclass.value?.let {
                        var mRecordRapeseed = AverageValueUtils.AverageValueRecordRapeseed(mRecordRiceFragment.mRecordRapeseedList, it)
                        if(null != mRecordRapeseed){
                            mRecordRiceFragment.UpdateDataMainClass(mMainClass.value.toString())
                        }
                    }
                }
            }
        }
    }


    /**
     * 选中打印
     */
    fun SelectPrint() {
        if(InitializeUtils.isLinkPrinter()){
            Thread{
                when (mMainClass.value) {
                    "稻谷" -> {
                        mSubclass.value?.let {
                            RicePrintUtils.RicPrinting(mRecordRiceFragment.mRecordRiceList, it)
                        }
                    }
                    "小麦" -> {
                        mSubclass.value?.let {
                            WheatPrintUtils.WheatPrinting(mRecordRiceFragment.mRecordWheatList,it)
                        }
                    }
                    "玉米" -> {
                        mSubclass.value?.let {
                            CornPrintUtils.CornPrinting(mRecordRiceFragment.mRecordCornList,it)
                        }
                    }
                    "大米" -> {
                        mSubclass.value?.let {
                            DaMiPrintUtils.DaMiPrinting(mRecordRiceFragment.mRecordDaMiList,it)
                        }
                    }
                    "大豆" -> {
                        mSubclass.value?.let {
                            SoybeanPrintUtils.SoybeanPrinting(mRecordRiceFragment.mRecordSoybeanList,it)
                        }
                    }
                    "油菜籽" -> {
                        mSubclass.value?.let {
                            RapeseedPrintUtils.SoybeanPrinting(mRecordRiceFragment.mRecordRapeseedList,it)
                        }
                    }
                }
            }.start()
        }else{
            InitializeUtils.getDefaultMaker()?.show("正在链接打印机")
            InitializeUtils.LinkPrinterModel(ActivityUtils.getTopActivity())
        }
    }


    /**
     * 上传
     */
    fun uploading(){
        CommonDialog.showGP("上传中")
        Thread{
            when (mMainClass.value) {
                "稻谷" -> {
                    for (mRecordRice in mRecordRiceFragment.mRecordRiceList){

                        /**
                         * 上传
                         */
                        if(mRecordRice.select){
                            val NetworkSettings = SPUtils.getInstance().getString("NetworkSettings")
                            if(TextUtils.isEmpty(NetworkSettings)){
                                //外网上传稻谷天平数据
                                RiceUploadUtils.uploadRiceBalanceInfo(mRecordRice,null)
                            }else{
                                //内网上传稻谷天平数据
                                RiceUploadUtils.IntranetRiceBalanceInfo(mRecordRice,null)
                            }
                        }

                    }
                }
                "小麦" -> {
                    for (mRecordWheat in mRecordRiceFragment.mRecordWheatList){
                        /**
                         * 上传
                         */
                        if(mRecordWheat.select) {
                            val NetworkSettings = SPUtils.getInstance().getString("NetworkSettings")
                            if (TextUtils.isEmpty(NetworkSettings)) {
                                //外网上传小麦天平数据
                                WheatUploadUtils.uploadWheatBalanceInfo(mRecordWheat, null)
                            } else {
                                //内网上传小麦天平数据
                                WheatUploadUtils.IntranetWheatBalanceInfo(mRecordWheat, null)
                            }
                        }
                    }
                }
                "玉米" -> {
                    for (mRecordCorn in mRecordRiceFragment.mRecordCornList){
                        /**
                         * 上传
                         */
                        if(mRecordCorn.select) {
                            val NetworkSettings = SPUtils.getInstance().getString("NetworkSettings")
                            if(TextUtils.isEmpty(NetworkSettings)){
                                //外网上传玉米天平数据
                                CornUploadUtils.uploadCornBalanceInfo(mRecordCorn,null)
                            }else{
                                //内网上传玉米天平数据
                                CornUploadUtils.IntranetCornBalanceInfo(mRecordCorn,null)
                            }
                        }

                    }
                }
                "大米" -> {
                    for (mRecordDaMi in mRecordRiceFragment.mRecordDaMiList){
                        /**
                         * 上传
                         */
                        if(mRecordDaMi.select) {
                            val NetworkSettings = SPUtils.getInstance().getString("NetworkSettings")
                            if (TextUtils.isEmpty(NetworkSettings)) {
                                //外网上传大米天平数据
                                DaMiUploadUtils.uploadTrueRiceBalanceInfo(mRecordDaMi, null)
                            } else {
                                //内网上传大米天平数据
                                DaMiUploadUtils.IntranetUploadTrueRiceBalanceInfo(mRecordDaMi, null)
                            }
                        }
                    }
                }
                "大豆" -> {
                    for (mRecordSoybean in mRecordRiceFragment.mRecordSoybeanList){
                        /**
                         * 上传
                         */
                        if(mRecordSoybean.select) {
                            val NetworkSettings = SPUtils.getInstance().getString("NetworkSettings")
                            if (TextUtils.isEmpty(NetworkSettings)) {
                                //外网上传大豆天平数据
                                SoybeanUploadUtils.uploadSoybeanBalanceInfo(mRecordSoybean, null)
                            } else {
                                //内网上传大豆天平数据
                                SoybeanUploadUtils.IntranetSoybeanBalanceInfo(mRecordSoybean, null)
                            }
                        }
                    }
                }
                "油菜籽" -> {
                    for (mRecordRapeseed in mRecordRiceFragment.mRecordRapeseedList){
                        /**
                         * 上传
                         */
                        if(mRecordRapeseed.select) {
                            val NetworkSettings = SPUtils.getInstance().getString("NetworkSettings")
                            if (TextUtils.isEmpty(NetworkSettings)) {
                                //外网上传油菜籽天平数据
                                RapeseedUploadUtils.uploadSoybeanBalanceInfo(mRecordRapeseed, null)
                            } else {
                                //内网上传油菜籽天平数据
                                RapeseedUploadUtils.IntranetRapeseedBalanceInfo(
                                    mRecordRapeseed,
                                    null
                                )
                            }
                        }
                    }
                }
            }
        }.start()
    }


    /**
     * 主菜单
     */
    fun MainMenu() {
        ActivityUtils.finishActivity(RecordActivity::class.java)
    }
}